//
//  LoginViewController.m
//  OneWordNew
//
//  Created by qianfeng on 15-7-6.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "LoginViewController.h"
#import "LZXHttpRequest.h"
#import "RegeditViewController.h"
#import "GivemePassViewController.h"
#import "MyPageViewController.h"
#import "PinLunViewController.h"
@interface LoginViewController ()
{
    LZXHttpRequest *_httpRequest;
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _httpRequest=[[LZXHttpRequest alloc] init];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.mimaText resignFirstResponder];
    [self.emailText resignFirstResponder];

}
- (void)showAlertViewWithTitle:(NSString *)title message:(NSString *)message {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)loginClick:(UIButton *)sender {
    if (self.emailText.text==nil||self.mimaText.text==nil) {
        [self showAlertViewWithTitle:@"警告" message:@"用户名或者密码不能为空"];
        return;
    }
    NSString *username=[self.emailText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *password=[self.mimaText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dict=@{@"username":username,
                        @"key":@"3a4b2a12539a916c040d069ae8ac8310",
                        @"password":password};
    NSString *url=[kloginUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [_httpRequest postRequestWithUrl:url param:dict contentType:@"application/x-www-form-urlencoded" success:^(LZXHttpRequest *httpRequest) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:_httpRequest.downloadData options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] isEqual:@(0)]) {
//            [[NSUserDefaults standardUserDefaults] setObject:dict[@"token"] forKey:@"token"];
//            [[NSUserDefaults standardUserDefaults] synchronize];
          //  NSLog(@"%@",dict[@"token"]);
            NSDictionary *data=dict[@"data"];
            MyPageViewController *mypag=[[MyPageViewController alloc] init];
            mypag.imagestr=data[@"avatar"];
            mypag.namestr=data[@"nickname"];
            mypag.idd=data[@"id"];
            
            PinLunViewController *pinl=[[PinLunViewController alloc] init];
            pinl.token=dict[@"token"];
            pinl.userId=data[@"id"];
            
            NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
            [ud setObject:dict[@"token"] forKey:@"token"];
            [ud setObject:data[@"nickname"] forKey:@"nickname"];
            [ud setObject:@"" forKey:@"bool"];
            [ud synchronize];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"secendOut" object:nil];
        //    NSLog(@"%@",pinl.token);
            
            [self.navigationController pushViewController:mypag animated:YES];
        }else{
        
            [self showAlertViewWithTitle:@"警告" message:dict[@"msg"]];
        }
        
        
    } failed:^(NSError *error) {
        [self showAlertViewWithTitle:@"警告" message:dict[@"msg"]];
    }];
    
}

- (IBAction)zhuceClick:(UIButton *)sender {
    RegeditViewController *reg=[[RegeditViewController alloc] init];
    
    [self.navigationController pushViewController:reg animated:YES];
    
}

- (IBAction)losemimaClick:(UIButton *)sender {
    GivemePassViewController *giv=[[GivemePassViewController alloc] init];
    
    [self.navigationController pushViewController:giv animated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden=NO;
}
@end
